package ThreadPoolnio1.Client;

import ThreadPoolnio1.NIOClient;
import ThreadPoolnio1.NetPrintStream;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.PrintWriter;
import java.net.Socket;

public class ClientGUI {
    static Socket s = null;
    static String msg = null;
    static PrintWriter out = null;
    public ClientGUI() {
        btnconnect.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                NetPrintStream printStream = new NetPrintStream(System.out, txtAccept);
                System.setErr(printStream);
                System.setOut(printStream);
                String ip = txtServer.getText();
                int port = Integer.parseInt(txtPort.getText());
             //  client = new Client(ip, port);    //IO操作
                client = new NIOClient(ip, port);     //NIO操作
            }
        });

        btnSend.addActionListener(new ActionListener() {
                                      @Override
                                      public void actionPerformed(ActionEvent e) {
                                          if (client != null) {
                    client.sendMsg(txtSend.getText());
                    txtSend.setText(null);
                } else
                    JOptionPane.showConfirmDialog(frame, "请先连接服务器", "出错了！",JOptionPane.PLAIN_MESSAGE);
                                      }
                                  });
        btnSendfile.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (client != null) {
                    JFileChooser fileChooser = new JFileChooser();
                    fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
                    if (fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
                        String file = fileChooser.getSelectedFile().getAbsolutePath();
                        client.sendFile(file);
                    }
                } else
                    JOptionPane.showConfirmDialog(frame, "请先连接服务器", "出错了", JOptionPane.PLAIN_MESSAGE);
            }
        });
        btndiscoonect.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                JOptionPane.showConfirmDialog(frame, "断开连接成功！", "退出", JOptionPane.PLAIN_MESSAGE);
                System.exit(0);
            }
        });
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("聊天客户端");
        frame.setContentPane(new ClientGUI().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800,600);
        frame.setLocationRelativeTo(null);
        //  frame.pack();
        frame.setVisible(true);
    }

    private JPanel panel1;
    private JTextArea txtAccept;
    private JTextField txtServer;
    private JTextField txtPort;
    private JButton btnconnect;
    private JButton btndiscoonect;
    private JTextArea txtSend;
    private JButton btnSendfile;
    private JButton btnSend;
   // private Client client = null;
    private NIOClient client = null;
    private static  JFrame frame = null;
}
